package com.my.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.admin.entity.FriendLink;
import com.my.admin.service.FriendLinkService;
import com.my.admin.util.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class FriendLinkController {

    @Autowired
    private FriendLinkService service;

    @PostMapping("friendLink/get")
    public Rest friendLinks(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize, @RequestBody FriendLink friendLink) {
        return Rest.ok().setData("page", service.page(new Page<>(pageNo, pageSize), new QueryWrapper<FriendLink>().
                like(StrUtil.isNotBlank(friendLink.getName()), "name", friendLink.getName())
                .like(StrUtil.isNotBlank(friendLink.getFriendDesc()), "friend_desc", friendLink.getFriendDesc())));
    }

    @PostMapping("friendLink")
    public Rest add(@RequestBody FriendLink friendLink) {
        if(service.save(friendLink)) {
            return Rest.ok("添加成功！");
        }else {
            return Rest.err("添加失败!");
        }
    }

    @PutMapping("friendLink")
    public Rest edit(@RequestBody FriendLink friendLink) {
        if(service.updateById(friendLink)) {
            return Rest.ok("修改成功！");
        }return Rest.err("修改失败！");
    }

    @DeleteMapping("friendLink")
    public Rest del(@RequestParam long id) {
        if(service.removeById(id)) {
            return Rest.ok("删除成功！");
        }return Rest.err("删除失败！");
    }

}
